<template>
  <div class="contentmain">
      <el-row>
          <el-col :span="24">
            <el-button type="danger" icon="el-icon-plus" @click="addgoodkind">添加商品分类</el-button>
          </el-col>
      </el-row>

      <el-row>
          <el-col :span="24">
            <el-table :data="td" style="width: 100%" v-loading="loading" element-loading-text="数据加载中...">
                <el-table-column prop="gcName" label="分类名称">
                </el-table-column>
                <el-table-column prop="gcSort" label="排序">
                </el-table-column>
                <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                    size="mini"
                    @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
                </el-table-column>
            </el-table>
          </el-col>
      </el-row>
    <GoodKind>
    </GoodKind>
  </div>
  
</template>
<script>
import GoodKind from 'components/GoodKind/GoodKind'
import axios from 'axios'
import qs from 'qs'
    export default{
        data(){
            return{
                td:[],
                loading:false
            }
        },
        computed:{//监视添加
            getstate(){
                return this.$store.state.goodkindnew;
            }
        },
        watch:{//如果弹窗有动静,去重新查一下表格,所以我在所有的弹窗都用了组件和vuex,易于监视
            getstate(va){
                if(va==true){
                    this.$store.state.goodkindnew=false
                    this.getData()
                }
            }
        },
        components:{
            GoodKind
        },
        methods:{
            handleEdit(i,r){//编辑提交组件
                this.$store.commit('ChangeGoodKindEditClass',r)
            },
            handleDelete(i,w){//删除
                var _w=w.gcId
                this.$confirm('此操作将删除该商品类型, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    var param = new URLSearchParams()
                    param.append("xcxGoodsClassId",_w)
                    axios.post('/deleteByXcxGoodsClassId.do',param).then(r=>{
                        // console.log(r)
                        if(r.data.code==200){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.getData()
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });          
                });
            },
            addgoodkind(){//新建商品分类
                this.$store.commit('ChangeGoodKindEditClass')
            },
            getData(v){//统一获取数据,添加到表格
                if(!this.$store.state.shiro){
                    return
                }
                var _this=this
                this.loading=true
                axios.get('/listXcxGoodsClass.do',{params: v }).then(r=>{
                    // console.log(r.data)
                    if(r.data.code!=200){
                        if(r.data.message=="not login"&&r.data.code==500){
                            _this.$message.error("访问者,请登录")
                            router.push('/Login');
                            return
                        }
                        _this.$message.error(r.data.message)
                        _this.loading=false
                        return
                    }
                    _this.td=[].concat(r.data.response.xcxGoodsClassList)
                    _this.loading=false
                })
            }
        },
        mounted(){//获取数据
            this.getData()
        },
        beforeCreate(){//判断权限
            let _this=this
            if(!this.$store.state.shiro){
                _this.$router.push('/full')
            }
        }
    }
</script>
<style>
.el-dialog__header {
    padding: 10px;
    background-color: #20a0ff !important;
}
.el-dialog__title{
    color: #fff !important;
}
</style>
